<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="能力分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <j-dict-select-tag
                type="list"
                v-model="model.categoryId"
                dictCode="ht_ability_category,name,id"
                placeholder="请选择能力分类"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="店铺ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopId">
              <j-dict-select-tag
                type="list"
                v-model="model.shopId"
                dictCode="ht_shop,name,id"
                placeholder="请选择店铺ID"
                :disabled="isDisabledAuth('ability-demand:shop-permission')"
              />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="SEO关键字" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seoKeywords">
              <a-textarea v-model="model.seoKeywords" rows="4" placeholder="请输入SEO关键字" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="SEO描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seoDescription">
              <a-textarea v-model="model.seoDescription" rows="4" placeholder="SEO描述" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="副标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subname">
              <a-input v-model="model.subname" placeholder="请输入副标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
              <a-input-number v-model="model.sort" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="首页排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="homeSort">
              <a-input-number v-model="model.homeSort" placeholder="请输入首页排序" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="主图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cover">
              <j-image-upload isMultiple v-model="model.cover"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片集" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="images">
              <j-image-upload isMultiple v-model="model.images"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-textarea v-model="model.description" rows="4" placeholder="请输入描述" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="详细内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <j-editor v-model="model.content" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="期望报价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="offerPrice">
              <a-input-number v-model="model.offerPrice" placeholder="请输入期望报价" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="采购数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseCount">
              <a-input-number v-model="model.purchaseCount" placeholder="请输入采购数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="需求状态： 0-求购中 1-已获得 2-求购失败"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="demandStatus"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.demandStatus"
                dictCode="ability_demand_status"
                placeholder="请选择需求状态： 0-求购中 1-已获得 2-求购失败"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发布日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="releaseDate">
              <j-date placeholder="请选择发布日期" v-model="model.releaseDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="截止日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expireDate">
              <j-date placeholder="请选择截止日期" v-model="model.expireDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col v-has="'ability-demand:home-recommended-permission'" :span="24">
            <a-form-model-item
              label="是否为首页推荐 0-不推荐 1-推荐"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="isHomeRecommended"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.isHomeRecommended"
                dictCode="is_home_recommended"
                placeholder="请选择是否为首页推荐 0-不推荐 1-推荐"
              />
            </a-form-model-item>
          </a-col>
          <div v-has="'ability-demand:audit-permission'">
            <a-col :span="24">
              <a-form-model-item label="审批人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auditUsername">
                <a-input v-model="model.auditUsername" placeholder="请输入审批人"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item
                label="审核状态：0-未审核 1-审核中 2-已通过 3-已驳回"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="auditStatus"
              >
                <j-dict-select-tag
                  type="list"
                  v-model="model.auditStatus"
                  dictCode="audit_status"
                  placeholder="请选择审核状态：0-未审核 1-审核中 2-已通过 3-已驳回"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="审批意见" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auditComments">
                <a-textarea v-model="model.auditComments" rows="4" placeholder="请输入审批意见" />
              </a-form-model-item>
            </a-col>
          </div>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { mapActions, mapState } from 'vuex'
import { DisabledAuthFilterMixin } from '@/mixins/DisabledAuthFilterMixin'

export default {
  name: 'HTAbilityDemandForm',
  mixins: [DisabledAuthFilterMixin],
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        shopId: undefined,
        purchaseCount: 1,
        demandStatus: 0,
        isHomeRecommended: 0,
        auditStatus: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        shopId: [{ required: true, message: '请输入店铺ID!' }],
        categoryId: [{ required: true, message: '请输入能力分类!' }],
        name: [{ required: true, message: '请输入标题!' }],
        // sort: [{ required: true, message: '请输入排序!' }],
        // homeSort: [{ required: true, message: '请输入首页排序!' }],
        content: [{ required: true, message: '请输入详细内容!' }],
        // offerPrice: [{ required: true, message: '请输入期望报价!' }],
        purchaseCount: [
          { required: true, message: '请输入采购数量!' },
          { pattern: /^-?\d+$/, message: '请输入整数!' }
        ],
        demandStatus: [{ required: true, message: '请输入需求状态： 0-求购中 1-已获得 2-求购失败!' }],
        releaseDate: [{ required: true, message: '请输入发布日期!' }],
        isHomeRecommended: [{ required: true, message: '请输入是否为首页推荐 0-不推荐 1-推荐!' }],
        auditStatus: [{ required: true, message: '请输入审核状态：0-未审核 1-审核中 2-已通过 3-已驳回!' }]
      },
      url: {
        add: '/ability/hTAbilityDemand/add',
        edit: '/ability/hTAbilityDemand/edit',
        queryById: '/ability/hTAbilityDemand/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    ...mapState('rolePermission', {
      shopPermission: state => state.shopPermission
    })
  },

  watch: {
    'shopPermission.shopId': {
      handler: function(newValue, oldValue) {
        if (newValue) {
          this.model.shopId = newValue
          this.$forceUpdate()
        }
      },
      immediate: true
    }
  },

  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    ...mapActions('rolePermission', ['getShopPermissionAction']),

    add() {
      this.getShopPermissionAction()
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>

